home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / sgi_extensions / point_parameters.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  10.0 KB  |  438 lines

  1. /*
  2.  * Copyright 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* point_parameters.c
  19.  *    This program demonstrates GL_SGIS_point_parameters.  It
  20.  *    allows you to change various point attributes dynamically.
  21.  *
  22.  *    <a/A> key    - increase/decrease the 'a' coefficient
  23.  *    <b/B> key    - increase/decrease the 'b' coefficient
  24.  *    <c/C> key    - increase/decrease the 'c' coefficient
  25.  *    <f/F> key    - increase/decrease the fade threshold size
  26.  *    <m/M> key    - increase/decrease the minimum point size
  27.  *    <x/X> key    - increase/decrease the maximum point size
  28.  *    Escape key    - exit the program
  29.  */
  30. #include <GL/gl.h>
  31. #include <GL/glu.h>
  32. #include <GL/glut.h>
  33.  
  34. #include <math.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37.  
  38. /* Function Prototypes */
  39.  
  40. GLvoid  initgfx( GLvoid );
  41. GLvoid  drawScene( GLvoid );
  42. GLvoid  reshape( GLsizei, GLsizei );
  43. GLvoid  keyboard( GLubyte, GLint, GLint );
  44.  
  45. GLvoid incFadeSize( GLvoid );
  46. GLvoid decFadeSize( GLvoid );
  47. GLvoid incCoefficient( GLint );
  48. GLvoid decCoefficient( GLint );
  49. GLvoid incMinSize( GLvoid );
  50. GLvoid decMinSize( GLvoid );
  51. GLvoid incMaxSize( GLvoid );
  52. GLvoid decMaxSize( GLvoid );
  53.  
  54. void printHelp( char * );
  55.  
  56. /* Global Definitions */
  57.  
  58. #define KEY_ESC    27    /* ascii value for the escape key */
  59.  
  60. /* Global Variables */
  61.  
  62. static GLfloat coefficients[3] = { 1.0, 0.0, 0.0 };
  63.  
  64. static GLfloat    fadeSize = 1.0;
  65. static GLfloat    minSize = 0.0;
  66. static GLfloat    maxSize = 0.0;
  67.  
  68. static GLfloat angle = 80.0;
  69.  
  70. static GLboolean pointParameterSupported = GL_TRUE;
  71.  
  72. void
  73. main( int argc, char *argv[] )
  74. {
  75.     GLsizei     width, height;
  76.  
  77.     glutInit( &argc, argv );
  78.  
  79.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  80.     height = glutGet( GLUT_SCREEN_HEIGHT );
  81.     glutInitWindowPosition( width/4, height/4 ); 
  82.     glutInitWindowSize( width/2, height/2 );
  83.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
  84.     glutCreateWindow( argv[0] );
  85.     
  86.     initgfx();
  87.  
  88.     glutKeyboardFunc( keyboard );
  89.     glutReshapeFunc( reshape );
  90.     glutDisplayFunc( drawScene ); 
  91.  
  92.     printHelp( argv[0] );
  93.  
  94.     glutMainLoop();
  95. }
  96.  
  97. GLvoid
  98. printHelp( char *progname )
  99. {
  100.     fprintf(stdout,"\n%s - demonstrates point parameters\n\n"
  101.         "<a/A> key    - increase/decrease the 'a' coefficient\n"
  102.         "<b/B> key    - increase/decrease the 'b' coefficient\n"
  103.         "<c/C> key    - increase/decrease the 'c' coefficient\n"
  104.         "<f/F> key    - increase/decrease the fade threshold size\n"
  105.         "<m/M> key    - increase/decrease the minimum point size\n"
  106.         "<x/X> key    - increase/decrease the maximum point size\n"
  107.         "Escape key        - exit the program\n\n",
  108.         progname
  109.     );
  110. }
  111.  
  112. GLvoid
  113. initgfx( GLvoid )
  114. {
  115.     glClearColor( 0.0, 0.0, 0.0, 0.0 );
  116.     glClear( GL_COLOR_BUFFER_BIT );
  117.  
  118.     if (!glutExtensionSupported("GL_SGIS_point_parameters")) {
  119.         pointParameterSupported = GL_FALSE;
  120.         fprintf( stderr,
  121.            "GL_SGIS_point_parameters not supported on this machine\n");
  122.     }
  123. }
  124.  
  125. GLvoid
  126. decFadeSize( GLvoid )
  127. {
  128. #ifdef    GL_SGIS_point_parameters
  129.     if (pointParameterSupported) {
  130.         if ( fadeSize > 0 ) fadeSize -= 0.1; 
  131.         printf( "fadeSize = %4.2f\n", fadeSize );
  132.         glPointParameterfSGIS( GL_POINT_FADE_THRESHOLD_SIZE_SGIS, fadeSize );
  133.         glutPostRedisplay();
  134.     } else {
  135.         fprintf( stderr,
  136.                    "GL_SGIS_point_parameters not supported on this machine\n");
  137.     }
  138. #else
  139.     fprintf( stderr,
  140.                    "GL_SGIS_point_parameters not supported on this machine\n");
  141. #endif
  142.  
  143. }
  144.  
  145. GLvoid
  146. incFadeSize( GLvoid )
  147. {
  148. #ifdef    GL_SGIS_point_parameters
  149.     if (pointParameterSupported) {
  150.         fadeSize += 0.1;
  151.         printf( "fadeSize = %4.2f\n", fadeSize );
  152.         glPointParameterfSGIS( GL_POINT_FADE_THRESHOLD_SIZE_SGIS, fadeSize );
  153.         glutPostRedisplay();
  154.     } else {
  155.         fprintf( stderr,
  156.                    "GL_SGIS_point_parameters not supported on this machine\n");
  157.     }
  158. #else
  159.     fprintf( stderr,
  160.                    "GL_SGIS_point_parameters not supported on this machine\n");
  161. #endif
  162. }
  163.  
  164. GLvoid
  165. decCoefficient( GLint i )
  166. {
  167. #ifdef    GL_SGIS_point_parameters
  168.     if (pointParameterSupported) {
  169.         if ( coefficients[i] > 0 ) coefficients[i] -= 0.1; 
  170.         printf( "coefficients[%d] = %4.2f\n", i, coefficients[i] );
  171.         glPointParameterfvSGIS( GL_DISTANCE_ATTENUATION_SGIS, coefficients );
  172.         glutPostRedisplay();
  173.     } else {
  174.         fprintf( stderr,
  175.                    "GL_SGIS_point_parameters not supported on this machine\n");
  176.     }
  177. #else
  178.     fprintf( stderr,
  179.                    "GL_SGIS_point_parameters not supported on this machine\n");
  180. #endif
  181. }
  182.  
  183. GLvoid
  184. incCoefficient( GLint i )
  185. {
  186. #ifdef    GL_SGIS_point_parameters
  187.     if (pointParameterSupported) {
  188.         coefficients[i] += 0.1;
  189.         printf( "coefficients[%d] = %4.2f\n", i, coefficients[i] );
  190.         glPointParameterfvSGIS( GL_DISTANCE_ATTENUATION_SGIS, coefficients );
  191.         glutPostRedisplay();
  192.     } else {
  193.         fprintf( stderr,
  194.                    "GL_SGIS_point_parameters not supported on this machine\n");
  195.     }
  196. #else
  197.     fprintf( stderr,
  198.                    "GL_SGIS_point_parameters not supported on this machine\n");
  199. #endif
  200. }
  201.  
  202. GLvoid
  203. decMinSize( GLvoid )
  204. {
  205. #ifdef    GL_SGIS_point_parameters
  206.     if (pointParameterSupported) {
  207.         if ( minSize > 0 ) minSize -= 0.1; 
  208.         printf( "minSize = %4.2f\n", minSize );
  209.         glPointParameterfSGIS( GL_POINT_SIZE_MIN_SGIS, minSize );
  210.         glutPostRedisplay();
  211.     } else {
  212.         fprintf( stderr,
  213.                    "GL_SGIS_point_parameters not supported on this machine\n");
  214.     }
  215. #else
  216.     fprintf( stderr,
  217.                    "GL_SGIS_point_parameters not supported on this machine\n");
  218. #endif
  219. }
  220.  
  221. GLvoid
  222. incMinSize( GLvoid )
  223. {
  224. #ifdef    GL_SGIS_point_parameters
  225.     if (pointParameterSupported) {
  226.         minSize += 0.1; 
  227.         printf( "minSize = %4.2f\n", minSize );
  228.         glPointParameterfSGIS( GL_POINT_SIZE_MIN_SGIS, minSize );
  229.         glutPostRedisplay();
  230.     } else {
  231.         fprintf( stderr,
  232.                    "GL_SGIS_point_parameters not supported on this machine\n");
  233.     }
  234. #else
  235.     fprintf( stderr,
  236.                    "GL_SGIS_point_parameters not supported on this machine\n");
  237. #endif
  238. }
  239.  
  240. GLvoid
  241. decMaxSize( GLvoid )
  242. {
  243. #ifdef    GL_SGIS_point_parameters
  244.     if (pointParameterSupported) {
  245.         if ( maxSize > 0 ) maxSize -= 0.1; 
  246.         printf( "maxSize = %4.2f\n", maxSize );
  247.         glPointParameterfSGIS( GL_POINT_SIZE_MAX_SGIS, maxSize );
  248.         glutPostRedisplay();
  249.     } else {
  250.         fprintf( stderr,
  251.                    "GL_SGIS_point_parameters not supported on this machine\n");
  252.     }
  253. #else
  254.     fprintf( stderr,
  255.                    "GL_SGIS_point_parameters not supported on this machine\n");
  256. #endif
  257. }
  258.  
  259. GLvoid
  260. incMaxSize( GLvoid )
  261. {
  262. #ifdef    GL_SGIS_point_parameters
  263.     if (pointParameterSupported) {
  264.         maxSize += 0.1; 
  265.         printf( "maxSize = %4.2f\n", maxSize );
  266.         glPointParameterfSGIS( GL_POINT_SIZE_MAX_SGIS, maxSize );
  267.         glutPostRedisplay();
  268.     } else {
  269.         fprintf( stderr,
  270.                    "GL_SGIS_point_parameters not supported on this machine\n");
  271.     }
  272. #else
  273.     fprintf( stderr,
  274.                    "GL_SGIS_point_parameters not supported on this machine\n");
  275. #endif
  276. }
  277.  
  278. GLvoid 
  279. keyboard( GLubyte key, GLint x, GLint y )
  280. {
  281.     switch (key) {
  282.     case 'a':    /* decrement coefficient a */
  283.         decCoefficient(0);
  284.         break;
  285.     case 'A':     /* increment coefficient a */
  286.         incCoefficient(0);
  287.         break;
  288.     case 'b':    /* decrement coefficient b */
  289.         decCoefficient(1);
  290.         break;
  291.     case 'B': 
  292.         incCoefficient(1);
  293.         break;
  294.     case 'c':    /* decrement coefficient c */
  295.         decCoefficient(2);
  296.         break;
  297.     case 'C': 
  298.         incCoefficient(2);
  299.         break;
  300.     case 'f':    /* decrement fade size */
  301.         decFadeSize();
  302.         break;
  303.     case 'F': 
  304.         incFadeSize();
  305.         break;
  306.     case 'm':    /* decrement min size */
  307.         decMinSize();
  308.         break;
  309.     case 'M': 
  310.         incMinSize();
  311.         break;
  312.     case 'x':    /* decrement max size */
  313.         decMaxSize();
  314.         break;
  315.     case 'X': 
  316.         incMaxSize();
  317.         break;
  318.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  319.         exit(0);
  320.     }
  321. }
  322.  
  323. GLvoid
  324. reshape( GLsizei width, GLsizei height )
  325. {
  326.     GLdouble    aspect;
  327.  
  328.     glViewport( 0, 0, width, height );
  329.  
  330.     aspect = (GLdouble) width / (GLdouble) height;
  331.  
  332.     glMatrixMode( GL_PROJECTION );
  333.     glLoadIdentity();
  334.     glOrtho (0.0,1.0,0.0,1.0,1.0,-1.0);
  335.     glMatrixMode( GL_MODELVIEW );
  336.     glLoadIdentity();
  337. }
  338.  
  339. static void Draw_Points0 (void)
  340. {
  341.     glPointSize (3);
  342.     glBegin (GL_POINTS);
  343.     glVertex4f (0.12,0.11,0.0,1.0);
  344.     glVertex4f (0.14,0.12,0.0,1.0);
  345.     glVertex4f (0.16,0.13,0.0,1.0);
  346.     glVertex4f (0.18,0.14,0.0,1.0);
  347.     glVertex4f (0.20,0.15,0.0,1.0);
  348.     glVertex4f (0.22,0.16,0.0,1.0);
  349.     glVertex4f (0.24,0.17,0.0,1.0);
  350.     glEnd();
  351.     glTranslatef (0.0,0.1,0.0);
  352. }
  353.  
  354.  
  355. static void Draw_Points1 (void)
  356. {
  357.     glPointSize(2);
  358.  
  359.     glBegin(GL_POINTS);
  360.     glVertex4f (0.10,0.10,0.0,1.0);
  361.     glEnd ();
  362.  
  363.     glPointSize (3);
  364.  
  365.     glBegin (GL_POINTS);
  366.     glVertex4f (0.12,0.11,0.0,1.0);
  367.     glVertex4f (0.14,0.12,0.0,1.0);
  368.     glEnd();
  369.  
  370.     glPointSize (4);
  371.  
  372.     glBegin (GL_POINTS);
  373.     glVertex4f (0.16,0.13,0.0,1.0);
  374.     glVertex4f (0.18,0.14,0.0,1.0);
  375.     glVertex4f (0.20,0.15,0.0,1.0);
  376.     glEnd();
  377.  
  378.     glTranslatef (0.0,0.1,0.0);
  379. }
  380.  
  381.  
  382. GLvoid
  383. drawScene( GLvoid )
  384. {
  385.     glClear( GL_COLOR_BUFFER_BIT );
  386.  
  387.     glPushMatrix();
  388.  
  389.     glColor4f (1.0,1.0,1.0,1.0);
  390.  
  391.     /* draw some aliased points */
  392.     Draw_Points0 ();
  393.  
  394.     /* move over a bit and draw the same points,
  395.      * with antialiasing */
  396.  
  397.     glBlendFunc (GL_SRC_ALPHA,GL_ONE);
  398.     glEnable (GL_BLEND);
  399.     glEnable (GL_POINT_SMOOTH);
  400.     Draw_Points0 ();
  401.     glBlendFunc (GL_ONE,GL_ZERO);
  402.     glDisable (GL_BLEND);
  403.     glDisable (GL_POINT_SMOOTH);
  404.  
  405.     /* draw some more aliased points */
  406.     Draw_Points1 ();
  407.  
  408.     /* move over a bit and draw the same points,
  409.      * with antialiasing */
  410.  
  411.     glBlendFunc (GL_SRC_ALPHA,GL_ONE);
  412.     glEnable (GL_BLEND);
  413.     glEnable (GL_POINT_SMOOTH);
  414.     Draw_Points1 ();
  415.     glBlendFunc (GL_ONE,GL_ZERO);
  416.     glDisable (GL_BLEND);
  417.     glDisable (GL_POINT_SMOOTH);
  418.  
  419.     /* draw a few very large points */
  420.  
  421.     glPointSize(8);
  422.     glBegin(GL_POINTS);
  423.     glVertex4f (0.10,0.10,0.0,1.0);
  424.     glEnd ();
  425.  
  426.     glTranslatef (0.0,0.1,0.0);
  427.  
  428.     glBegin(GL_POINTS);
  429.     glVertex4f (0.10,0.10,0.0,1.0);
  430.     glEnd ();
  431.  
  432.     glPopMatrix();
  433.  
  434.     glutSwapBuffers();
  435.  
  436.     checkError("drawScene");
  437. }
  438.